package com.ledim.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.activity.base.LedimBaseListActivity;
import com.ledim.adapter.Card9ListAdapter;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.bean.base.BaseListResponse;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.FrescoUtil;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/11/28.
 * 专题列表页接口 卡片9
 */

public class Card9ListActivity extends LedimBaseListActivity {

    private String mGroupId;
    private rx.Observer mListObsevers;
    private String mGroupPhoto;
    private String mGroupDes;
    private String mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        ImageView backIv = findViewByIdExt(R.id.iv_back);
        backIv.setVisibility(View.VISIBLE);
        backIv.setOnClickListener(this);
        View headerView = getLayoutInflater().inflate(R.layout.card9_header_view, null);
        TextView groupDesTv = (TextView) headerView.findViewById(R.id.card_group_des);
        SimpleDraweeView groupIv =
                (SimpleDraweeView) headerView.findViewById(R.id.card_group_photo);
        if (TextUtils.isEmpty(mGroupPhoto)) {
            groupIv.setVisibility(View.GONE);
        } else {
            FrescoUtil.loadUrl(mGroupPhoto, groupIv);
        }
        if (TextUtils.isEmpty(mGroupDes)) {
            groupDesTv.setVisibility(View.GONE);
        } else {
            groupDesTv.setText(mGroupDes);
        }
        if (!TextUtils.isEmpty(mTitle)) {
            TextView titleTv = findViewByIdExt(R.id.tv_title);
            titleTv.setText(mTitle);
        }
        ((ListView) mAdapterView).addHeaderView(headerView);
    }

    @Override
    protected void initIntentData(Bundle bundle) {
        super.initIntentData(bundle);
        mGroupId = bundle.getString(ConstantUtil.ID);
        mGroupPhoto = bundle.getString(ConstantUtil.PHOTO);
        mGroupDes = bundle.getString(ConstantUtil.DES);
        mTitle = bundle.getString(ConstantUtil.TITLE);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected LedimBaseListAdapter getAdapter() {
        return new Card9ListAdapter(this, mGroupId);
    }

    @Override
    protected void getDatas() {
        if (mListObsevers == null) {
            mListObsevers = new LedimHttpObserver<BaseListResponse<LedimChoiceCardBean>>() {
                @Override
                public void onCompleted() {
                    super.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    setServerDataError();
                }

                @Override
                public void onNext(BaseListResponse<LedimChoiceCardBean> cards) {
                    if (cards != null && cards.success) {
                        initTotalPages(cards.paged.total);
                        setDatas(cards.data, cards.paged.more);
                    } else {
                        setServerDataError();
                    }
                }
            };
        }
        LedimApiServiceFactory.createService(ApiService.class)
                              .getCardsByGroupId(mGroupId, mLoadPage + 1, getPageSize())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(mListObsevers);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_card9_list;
    }

    @Override
    protected boolean enablePullRefresh() {
        return false;
    }
}
